package mobileann.mafamily.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.mobileann.mafamily.R;

import mobileann.mafamily.act.main.FS;
import mobileann.mafamily.service.UpdateService;
import android.R.integer;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.NoCopySpan.Concrete;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

/**
 * 功能:文件操作工具类
 * <br>用途:处理下载下来的文件
 * @annotation ADRIAN.2015.9.6.17.00
 */
public class FileUtilsForDown {
	private String SDPATH;
	private int FILESIZE = 8 * 1024;

	public String getSDPATH() {
		return SDPATH;
	}

	public FileUtilsForDown() {
		String path = FS.getInstance().getFilesDir().getAbsolutePath() + "/";
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			path = Environment.getExternalStorageDirectory() + "/";
		}
		SDPATH = path;
	}

	/**
	 * 在SD卡上创建文件
	 * 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public File createFile(String fileName) throws IOException {
		File file = new File(SDPATH + fileName);
		file.createNewFile();
		return file;
	}

	/**
	 * 在SD卡上删除文件
	 * 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public boolean deleteFile(String fileName) throws IOException {
		File file = new File(SDPATH + fileName);
		return file.delete();
	}

	/**
	 * 在SD卡上创建目录
	 * 
	 * @param dirName
	 * @return
	 */
	public File createDir(String dirName) {
		File dir = new File(SDPATH + dirName);
		dir.mkdir();
		return dir;
	}

	/**
	 * 判断SD卡上的文件夹是否存在
	 * 
	 * @param fileName
	 * @return
	 */
	public boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		return file.exists();
	}

	/**
	 * 获取SD卡上的文件大小
	 * 
	 * @param fileName
	 * @return
	 */
	public long getFileSize(String fileName) {
		File file = new File(SDPATH + fileName);
		return file.length();
	}

	private int current = 0;
	private int progress = 0;
	/**
	 * 将一个InputStream里面的数据写入到SD卡中
	 * 
	 * @param path
	 * @param fileName
	 * @param input
	 * @return
	 * @throws IOException
	 */
	public File write3FileFromInput(String path, String fileName,
			InputStream input, int length,ProgressListener progressListener) throws IOException {
		File file = null;
		OutputStream output = null;
		{
			createDir(path);
			file = createFile(path + fileName);
			output = new FileOutputStream(file);
			byte[] buffer = new byte[FILESIZE];
			int count = 0;
			while ((count = input.read(buffer, 0, FILESIZE)) > 0) {
				output.write(buffer, 0, count);
				current += count;
				progress = current * 100 / length;
				progressListener.getProgress(progress);
			}
			output.close();
		}
		return file;
	}
	public interface ProgressListener{
    	void getProgress(int progress);
    }

	/**
	 * 将一个InputStream里面的数据写入到SD卡中
	 * 
	 * @param path
	 * @param fileName
	 * @param input
	 * @return
	 * @throws IOException
	 */
	public File write2FileFromInput(String path, String fileName,
			InputStream input) throws IOException {
		File file = null;
		OutputStream output = null;
		{
			createDir(path);
			file = createFile(path + fileName);
			output = new FileOutputStream(file);
			byte[] buffer = new byte[FILESIZE];
			int count = 0;
			while ((count = input.read(buffer, 0, FILESIZE)) > 0) {
				output.write(buffer, 0, count);
			}
			output.close();
		}
		return file;
	}
}
